//
//  BDJTabbarController.m
//  BuDeJie
//
//  Created by TFAppleWork-Summer on 2018/3/19.
//  Copyright © 2018年 TFAppleWork-Summer. All rights reserved.
//

#import "BDJTabbarController.h"
#import "BDJMeViewController.h"
#import "BDJNewViewController.h"
#import "BDJEssenceViewController.h"
#import "BDJPublishViewController.h"
#import "BDJFriendTreadViewController.h"

#import "UIImage+Image.h"
#import "BDJTabBar.h"
#import "BDJNavigationController.h"
@interface BDJTabbarController ()

@end

@implementation BDJTabbarController

// 只会调用一次
+(void)load{
    
    /*
     appearance:
     1 遵守了UIAppearance 协议，并且实现了appearance 方法，就可以调用这个方法
     2 哪些属性可以通过appearance 设置
     被UI_APPEARANCE_SELECTOR这个修饰过的，才可以设置
     */
    
    // 获取整个应用程序下的UITabBarItem
//    UITabBarItem *item = [UITabBarItem appearance];
    // 获取哪些类中的UITabBarItem
    UITabBarItem *item = [UITabBarItem appearanceWhenContainedInInstancesOfClasses:@[[self class]]];
    //设置选中按钮标题的颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    [attrs setValue:[UIColor blackColor] forKey:NSForegroundColorAttributeName];
    [item setTitleTextAttributes:attrs forState:UIControlStateSelected ];
    
     // 设置字体尺寸 :只有正常状态下才有效果
     NSMutableDictionary *attrsNor = [NSMutableDictionary dictionary];
    [attrsNor setValue:[UIFont systemFontOfSize:13] forKey:NSFontAttributeName];
     [item setTitleTextAttributes:attrsNor forState:UIControlStateNormal];
    
}

/*
 1 选中按钮图片被渲染
 a.在Assets.xcassets中找到选中图片，并且将图片的“Render as” 改为Original Image
 b.代码方式 : 取出图片改为original图片的模式
 UIImage *essenceSelectImage = [UIImage imageNamed:@"tabHomeSel"];
 essenceSelectImage = [essenceSelectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
 2 选中按钮标题的颜色：黑色  标题字体大
 
 3 发布按钮显示不出来:不能修改图片尺寸，让发布图片居中
 a : 图片太大，系统帮你渲染 = >使用原图不渲染 => 位置不对 = > tabbaritem.imageInset = > 高亮状态达不到
 */

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //2.1 添加子控制器
    [self p_setUpAllChildViewController];
    // 2.2 设置tabbar按钮的内容 -> 由对应的子控制器的tabBarItem属性确定
    [self p_setUpAllTitleBtn];
    [self setValue:[[BDJTabBar alloc] init] forKeyPath:@"tabBar"];
    
    
}

-(void)p_setUpAllChildViewController{
    // 精华
    BDJEssenceViewController *essenceVC = [[BDJEssenceViewController alloc]init];
    BDJNavigationController *essenceNav = [[BDJNavigationController alloc]initWithRootViewController:essenceVC];
    [self addChildViewController:essenceNav];
    // 新帖
    BDJNewViewController *newVC = [[BDJNewViewController alloc]init];
    BDJNavigationController *newNav = [[BDJNavigationController alloc]initWithRootViewController:newVC];
    [self addChildViewController:newNav];
    // 关注
    BDJFriendTreadViewController *friendTreadVC = [[BDJFriendTreadViewController alloc]init];
    BDJNavigationController *FriendTreadNav = [[BDJNavigationController alloc]initWithRootViewController:friendTreadVC];
    [self addChildViewController:FriendTreadNav];
    // 我
    BDJMeViewController *meVC = [[BDJMeViewController alloc]init];
    BDJNavigationController *meNav = [[BDJNavigationController alloc]initWithRootViewController:meVC];
    [self addChildViewController:meNav];
    
}

-(void)p_setUpAllTitleBtn{
    // 0:精华
    UINavigationController *essenceNav = self.childViewControllers[0];
    essenceNav.tabBarItem.title = @"精华";
    essenceNav.tabBarItem.image = [UIImage imageNamed:@"tabHome"];
    essenceNav.tabBarItem.selectedImage = [UIImage imageOriginalWithName:@"tabHomeSel"];
    
  
    //1 最新
    UINavigationController *newNav = self.childViewControllers[1];
    newNav.tabBarItem.title = @"最新";
    newNav.tabBarItem.image = [UIImage imageNamed:@"tabDiscovery"];
    newNav.tabBarItem.selectedImage = [UIImage imageOriginalWithName:@"tabDiscoverySel"];
    // 关注
    UINavigationController *FriendTreadNav = self.childViewControllers[2];
    FriendTreadNav.tabBarItem.title = @"关注";
    FriendTreadNav.tabBarItem.image = [UIImage imageNamed:@"tabTimeBook"];
    FriendTreadNav.tabBarItem.selectedImage = [UIImage imageOriginalWithName:@"tabTimeBookSel"];
    // 我
    UINavigationController *meNav = self.childViewControllers[3];
    meNav.tabBarItem.title = @"我";
    meNav.tabBarItem.image = [UIImage imageNamed:@"tabMine"];
    meNav.tabBarItem.selectedImage = [UIImage imageOriginalWithName:@"tabMineSel"];
   
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
